package com.lanou.service.impl;

import com.lanou.entity.*;
import com.lanou.mapper.TbContentCategoryMapper;
import com.lanou.mapper.TbContentMapper;
import com.lanou.service.HomeShowService;
import com.lanou.utils.HttpClientUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by QuChao on 2018/11/30
 */
@Service
public class HomeShowServiceImpl implements HomeShowService {

    @Autowired
    private TbContentCategoryMapper tbContentCategoryMapper;
    @Autowired
    private TbContentMapper tbContentMapper;

    @Value("${INDEX_CONTENT_JSON_URL}")
    private String INDEX_CONTENT_JSON_URL;

    @Value("${ITEM_JSON_URL}")
    private String ITEM_JSON_URL;

    @Value("${ITEM_DESC_JSON_URL}")
    private String ITEM_DESC_JSON_URL;

    @Override

    public List<Map<String, Object>> getHomeContent() {
        TbContentCategoryExample tbContentCategoryExample = new TbContentCategoryExample();
        tbContentCategoryExample.createCriteria().andIsParentEqualTo(false);
        List<TbContentCategory> list = tbContentCategoryMapper.selectByExample(tbContentCategoryExample);

        List<Map<String, Object>> list1 = new ArrayList<>();

        for (TbContentCategory tbContentCategory : list) {
            Map<String, Object> map = new HashMap<>();
            List<TbContent> tbContents = HttpClientUtils.getList(INDEX_CONTENT_JSON_URL + tbContentCategory.getId(), TbContent.class);
            map.put("id", tbContentCategory.getId());
            map.put("name", tbContentCategory.getName());
            List<Map<String, Object>> list2 = new ArrayList<>();
            if ("轮播图".equals(tbContentCategory.getName())) {
                for (TbContent tbContent : tbContents) {
                    Map<String, Object> map1 = new HashMap<>();
                    map1.put("id", tbContent.getId());
                    map1.put("category_id", tbContent.getCategoryId());
                    map1.put("title", tbContent.getTitle());
                    map1.put("subTitle", tbContent.getSubTitle());
                    map1.put("titleDesc", tbContent.getTitleDesc());
                    map1.put("url", tbContent.getUrl());
                    map1.put("pic", tbContent.getPic());
                    map1.put("pic2", tbContent.getPic2());
                    map1.put("content", tbContent.getContent());
                    if (tbContent.getItemId() != null) {
                        TbItem tbItem = HttpClientUtils.getObject(ITEM_JSON_URL + tbContent.getItemId(), TbItem.class);
                        map1.put("item_id", tbItem.getId());
                        map1.put("item_title", tbItem.getTitle());
                        map1.put("item_sell_point", tbItem.getSellPoint());
                        map1.put("item_price", tbItem.getPrice());
                        String[] strings = tbItem.getImage().split(",");
                        int count = 0;
                        for (String string : strings) {
                            map1.put("item_image" + count, "http://" + string);
                            count++;
                        }
                        map1.put("item_cid", tbItem.getCid());
                    }
                    list2.add(map1);
                }
            } else {
                for (TbContent tbContent : tbContents) {
                    Map<String, Object> map1 = new HashMap<>();
                    map1.put("id", tbContent.getId());
                    map1.put("panelId", tbContent.getCategoryId());
                    map1.put("fullUrl", tbContent.getUrl());
                    if (tbContent.getItemId() != null) {
                        TbItem tbItem = HttpClientUtils.getObject(ITEM_JSON_URL + tbContent.getItemId(), TbItem.class);
                        map1.put("productId", tbItem.getId());
                        map1.put("productName", tbItem.getTitle());
                        map1.put("subTitle", tbItem.getSellPoint());
                        map1.put("salePrice", tbItem.getPrice() * 0.01);
                        String[] strings = tbItem.getImage().split(",");
                        map1.put("productImageBig", "http://" + strings[0]);
                        int count = 1;
                        for (String string : strings) {
                            if (count == 1) {
                                map1.put("picUrl", "http://" + string);
                            } else {
                                map1.put("picUrl" + count, "http://" + string);
                            }
                            count++;
                        }
                        map1.put("item_cid", tbItem.getCid());
                    }
                    list2.add(map1);
                }
            }

            map.put("tbContents", list2);
            list1.add(map);
        }
        return list1;
    }

    @Override
    public Map<String, Object> getDescById(Long productId) {
        TbItem tbItem = HttpClientUtils.getObject(ITEM_JSON_URL + productId, TbItem.class);
        Map<String, Object> map = new HashMap<>();
        map.put("productId", productId);
        map.put("salePrice", tbItem.getPrice() * 0.01);
        map.put("productName", tbItem.getTitle());
        map.put("subTitle", tbItem.getSellPoint());
        TbItemDesc tbItemDesc = HttpClientUtils.getObject(ITEM_DESC_JSON_URL + productId, TbItemDesc.class);
        map.put("detail", tbItemDesc.getItemDesc());

        String[] split = tbItem.getImage().split(",");
        map.put("productImageBig", "http://" + split[0]);

        List<String> list = new ArrayList<>();
        for (String s : split) {
            list.add("http://" + s);
        }
        map.put("productImageSmall", list);
        return map;
    }

}
